/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBeans;

import SessionBeans.UserSessionBean;
import entities.User;
import filesTableModel.TableItemInterface;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import sharingTableModel.SharedNodeData;
import sharingTableModel.SharedTableModel;

/**
 *
 * @author Tomas
 */
@ManagedBean(name = "sharingManagedBean")
@ViewScoped
public class SharingManagedBean {

    @EJB
    private UserSessionBean userSessionBean;
    private String userName;
    private boolean folder;
    private Long id;
    private String name;
    private int rights;
    private List<SharedNodeData> dates;
    private SharedTableModel sharedTableModel;
    private User ownerUser;

    /**
     * Creates a new instance of SharingManagedBean
     */
    public SharingManagedBean() {
    }

    @PostConstruct
    private void init() {
        sharedTableModel = new SharedTableModel();
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        ownerUser = (User) session.getAttribute("user");
        TableItemInterface selectedNode = (TableItemInterface) session.getAttribute("selectedNode");
        //session.removeAttribute("selectedNode");
        id = selectedNode.getId();
        name = selectedNode.getName();
        folder = selectedNode.isFolder();

        dates = sharedTableModel.loadData(id, folder);
    }

    @PreDestroy
    private void destroy() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        session.removeAttribute("selectedNode");
    }

    public int getRights() {
        return rights;
    }

    public void setRights(int rights) {
        this.rights = rights;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public List<SharedNodeData> getDatas() {
        return dates;
    }

    public void setDatas(List<SharedNodeData> datas) {
        this.dates = datas;
    }

    public boolean isFolder() {
        return folder;
    }

    public void setFolder(boolean folder) {
        this.folder = folder;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getPath() {
        return name;
    }

    public void setPath(String path) {
        this.name = path;
    }

    public void add() {
        if (!userSessionBean.checkUserName(userName)) {
            FacesContext.getCurrentInstance().addMessage("shUserName", new FacesMessage(FacesMessage.SEVERITY_ERROR, "User not exist!", "User with this username not exitst."));
            return;
        }

        sharedTableModel.add(id, folder, ownerUser.getId(), userSessionBean.findUser(userName).getId(), Long.valueOf(String.valueOf(rights)));

        dates = sharedTableModel.loadData(id, folder);
    }
    
    public void remove(SharedNodeData remove){
        dates.remove(remove);
        
        sharedTableModel.remove(remove.getId(), folder);
    }
}
